<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\models\Card;use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $isp app\models\Isp
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '运营商详情';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<table class="table table-striped table-bordered table-hover">
    <tr>
        <th colspan="2"><h4>基本信息</h4></th>
    </tr>
    <tr>
        <th>编号</th>
        <td><?php echo $isp->id;?></td>
    </tr>
    <tr>
        <th>名称</th>
        <td><?php echo Html::encode($isp->name);?></td>
    </tr>
    <tr>
        <th>接口</th>
        <td><?php echo Html::encode($isp->api->name);?></td>
    </tr>
    <tr>
        <th>接口参数</th>
        <td>
            <table class="table table-striped table-bordered table-hover">
                <?php $param = json_decode($isp->api->param, true);
                $data = json_decode($isp->data, true);
                foreach ($param as $p) {?>
                    <tr>
                        <th><?php echo Html::encode($p);?></th>
                        <td><?php echo Html::encode(isset($data[$p]) ? $data[$p] : '');?></td>
                    </tr>
                <?php }?>
            </table>
        </td>
    </tr>
    <tr>
        <th>创建时间</th>
        <td><?php echo Yii::$app->formatter->asDatetime($isp->create_time);?></td>
    </tr>
    <tr>
        <th colspan="2"><h4>卡统计</h4></th>
    </tr>
    <tr>
        <th>卡数量</th>
        <td><?php echo Html::a(Card::find()->andWhere(['isp_id' => $isp->id])->count(), ['/admin/card/list', 'search_api_id' => $isp->api_id, 'search_isp_id' => $isp->id]);?>
            <button type="button" class="btn btn-default btn-sm" onclick="syncIspCard(<?php echo $isp->id;?>)">同步所有卡信息</button>
        </td>
    </tr>
</table>
<script>
    /**
     * 同步运营商所有卡信息
     * @param id 运营商编号
     */
    function syncIspCard(id) {
        $.getJSON('<?php echo Url::to(['/admin/card/sync-isp-card']);?>', {id: id}, function (json) {
            if (callback(json)) {
                layer.msg('同步任务已经添加到队列。');
            }
        });
    }
</script>
